<template>
  <view class="container">
    <view v-if="tabList.length===2" class="tabs-box">
      <uni-segmented-control :current="current" :values="tabList" @clickItem="itemChange" />
    </view>
    <view class="goods-list">
      <view class="item" v-for="(item, index) in goodsData">
        <goods-number ref="goodsNumber" :itemList="item" :goodsType="type" :tenantId="tenantId"
          @getLists="initData"></goods-number>
      </view>
      <uni-load-more :status="isMore" />
    </view>
  </view>
</template>

<script>
  import { getDeliverGoodsData, getPullinGoodsData } from "@/api/system/goods.js"
  import { getTenantId, getDriverId, getOpenId } from '@/utils/auth';
  import { getTenantConfig } from '@/api/me.js';

  export default {
    data() {
      return {
        type: '',
        current: 0,
        tenantId: undefined,
        tabList: ['送货运单', '拉货运单'],
        goodsData: [],
        queryParam: {
          driverId: undefined,
          enterId: undefined,
          currentPage: 1,
          pageSize: 10,
          current: 1
        },
        isMore: 'more'
      }
    },
    onPullDownRefresh() {
      this.initData()
    },
    onReachBottom() {
      if (this.isMore === 'more') {
        this.queryParam.currentPage++;
        this.queryParam.current++;
        this.getDatas();
      }
    },
    onLoad(options) {
      this.type = options.type || ''
    },
    onShow() {
      this.$nextTick(() => {
        if (Array.isArray(this.$refs.goodsNumber)) {
          this.$refs.goodsNumber.forEach(ref => {
            if (ref && typeof ref.closePopupReupload === 'function') {
              ref.closePopupReupload();
            }
          });
        }
      })
      if (getTenantId()) {
        this.tenantId = getTenantId()
        this.getTenantConfig()
      }
    },
    methods: {
      async getTenantConfig() {
        const res = await getTenantConfig();
        const result = res?.data?.bizType || []
        if (res.code !== 0 || result.length === 0) {
          this.current = 0
          this.type = 'deliver'
          this.tabList = ['送货运单', '拉货运单']
          this.initData()
          return
        }
        this.tabList = []
        if (result.length === 1) {
          if (result.includes('1')) {
            this.type = 'deliver'
            this.initData()
          }
          if (result.includes('2')) {
            this.type = 'pull'
            this.initData()
          }
          return
        }
        if (result.length === 2) {
          this.type = this.type || 'deliver'
          this.tabList = ['送货运单', '拉货运单']
          if (this.type === 'deliver') {
            this.current = 0
          } else {
            this.current = 1
          }
          this.initData()
        }
      },
      itemChange({ currentIndex }) {
        this.current = currentIndex
        const tab = this.tabList[this.current]
        if (tab === '送货运单') {
          this.type = 'deliver'
        }
        if (tab === '拉货运单') {
          this.type = 'pull'
        }
        this.initData()
      },
      initData() {
        this.goodsData = []
        this.isMore = 'more'
        this.queryParam.currentPage = 1
        this.queryParam.current = 1;
        this.getDatas()
      },
      async getDatas() {
        let res = null;
        this.queryParam.driverId = getDriverId()
        this.queryParam.enterId = getTenantId()
        if (this.type === 'deliver') {
          res = await getDeliverGoodsData(this.queryParam)
        }
        if (this.type === 'pull') {
          res = await getPullinGoodsData(this.queryParam)
        }
        if (res.code == "200") {
          const list = res.data.rows || []
          const total = res.data.total || 0
          this.goodsData = [...this.goodsData, ...list];
          if (total == this.goodsData.length) {
            this.isMore = 'noMore';
          } else {
            this.isMore = 'more';
          }
        } else {
          this.goodsData = []
          this.isMore = 'noMore'
        }
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
      }
    }
  };
</script>

<style lang="scss" scoped>
  .container {
    .tabs-box {
      padding: 0 20rpx 20rpx 20rpx;
      background-color: #ffffff;
    }

    .goods-list {
      padding: 32rpx 20rpx 20rpx 20rpx;

      .item {
        margin-bottom: 20rpx;
      }
    }
  }
</style>